<?php
/**
 * 

 */


class db_gonglue extends db_base
{
	
 
	var $table_name = "gonglue";
	
	function db_gonglue()
	{
		parent::db_base();
	}
	
	
	function get_item_by_id($id)
	{
		$id = intval($id);
		
		$mc = mem_cache::get_instance();
	
		$key = $this->table_name."-get_item_by_id-{$id}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$where = "id = $id and status = 1";
			$content = $this->get_alllist($this->table_name,1,1,"",$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_DAY);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content[0];
		
	}
	
	
	
	
	function get_list_order_by_time($p,$pcount,$content_type="",$topic_id="",$title="",$create_time="",$order=0)
	{
		$pcount = intval($pcount);
		$p = intval($p);
		$topic_id = intval($topic_id);
		$title = mysql_escape_string($title);
		
		if($title)
		{
			$content_type ="";
		}
		
		$mc = mem_cache::get_instance();
	
		$key = $this->table_name."-get_list_order_by_time-{$p}-{$pcount}-{$content_type}-{$topic_id}-{$title}-{$create_time}-{$order}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$where_ary = array();
			if(strlen($content_type)>0)$where_ary[] = "content_type = $content_type";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			if($title)$where_ary[] = "title like '%{$title}%'";
			if($create_time)$where_ary[] = "create_time <= '{$create_time}'";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			if(!$order)
			{
				$order = "create_time desc";
			}
			else
			{
				$order = "create_time ";
			}
			$content = $this->get_alllist($this->table_name,$p,$pcount,$order,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_HOUR);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	function get_count($content_type="",$topic_id="",$title="")
	{
		
		$topic_id = intval($topic_id);
		$title = mysql_escape_string($title);
		
		if($title)
		{
			$content_type ="";
		}
		$mc = mem_cache::get_instance();
	
		$key = $this->table_name."-get_count-{$content_type}-{$topic_id}-{$title}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$where_ary = array();
			if(strlen($content_type)>0)$where_ary[] = "content_type = $content_type";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			if($title)$where_ary[] = "title like '%{$title}%'";
			$where_ary[] = "status = 1";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_listcount($this->table_name,$where);
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_DAY);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	function get_gonglue_list_biger_by_topic_and_create_time($p=1,$pcount=10,$topic_id="",$create_time="")
	{
		$pcount = intval($pcount);
		$p = intval($p);
		$topic_id = intval($topic_id);
		$create_time = mysql_escape_string($create_time);
		
		
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_gonglue_list_biger_by_topic_and_create_time-{$p}-{$pcount}-{$topic_id}-{$create_time}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$where_ary = array();
			if($create_time)$where_ary[] = " create_time >= '{$create_time}'";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where = join (" and ",$where_ary);
			
			$content = $this->get_alllist($this->table_name,$p,$pcount,"create_time",$where,"","id,title");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	function get_gonglue_list_smaller_by_topic_and_create_time($p=1,$pcount=10,$topic_id="",$create_time="")
	{
		$pcount = intval($pcount);
		$p = intval($p);
		$topic_id = intval($topic_id);
		$create_time = mysql_escape_string($create_time);
		
		$mc = mem_cache::get_instance();
	
		$key = CACHE_PREFIX_PUBU."-get_gonglue_list_smaller_by_topic_and_create_time-{$p}-{$pcount}-{$topic_id}-{$create_time}";
		$cache = $mc->get($key);
		if ($cache === false)
		{	
			$where_ary = array();
			if($create_time)$where_ary[] = " create_time <= '{$create_time}'";
			if($topic_id)$where_ary[] = "topic_id = $topic_id";
			$where = join (" and ",$where_ary);
			$content = $this->get_alllist($this->table_name,$p,$pcount,"create_time desc",$where,"","id,title");
			// 保存MC
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_5);
		}
		else
		{
			$content = unserialize($cache);
		}
		
		return $content;
		
	}
	
	
	
	
	function admin_get_one_by_id($id)
	{
		$id = intval($id);
		$where = "id = $id";
		$list = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
		
		return $list[0];
	}
	
	
	function admin_get_list_by_search($p=1,$pcount=20,$topic_id="")
	{
		$pcount = intval($pcount);
		$p = intval($p);
		$topic_id = intval($topic_id);
		
		
		$ary_where =  array();
		if($topic_id)
		{
			$ary_where[] = "topic_id = $topic_id";
		}
		
		$ary_where[] = "status = 1";
		
		
		$where = join(" and ",$ary_where);
		$list = $this->get_alllist($this->table_name,$p,$pcount,"",$where);
		
		return $list;
	}
	
	
	function admin_get_count_by_search($topic_id="")
	{
		$topic_id = intval($topic_id);
		
		$ary_where =  array();
		if($topic_id)
		{
			$ary_where[] = "topic_id = $topic_id";
		}
		
		$ary_where[] = "status = 1";
		
		
		$where = join(" and ",$ary_where);
		$list = $this->get_listcount($this->table_name,$where);
		
		return $list;
	}
	
	
	function admin_insert_gonglue($topic_id,$url,$get_type,$content_type)
	{
		// 插入数据
		$ary = array();

		$ary["topic_id"] = $topic_id;
		$ary["url"] = $url;
		$ary["get_type"] = $get_type;
		$ary["content_type"] = $content_type;
		
		$ary["status"] = 1;
		
		$ary["create_time"] = Date::get_date_time();
		$ary["create_ip"] = IP::get_client_ip_long();
		
		
		$ret = $this->insert($this->table_name, $ary);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $this->get_lastinsertid();
	}
	
	
	function admin_update_gonglue($id,$topic_id,$url,$get_type,$content_type)
	{
		$id = intval($id);
		// 插入数据
		$ary = array();

		$ary["topic_id"] = $topic_id;
		$ary["url"] = $url;
		$ary["get_type"] = $get_type;
		$ary["content_type"] = $content_type;
		$where = "id = $id";
		
		
		
		$ret = $this->update($this->table_name, $ary,$where);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $ret;
	}
	
	
	
	function admin_del_gonglue($id)
	{
		$id = intval($id);
		// 插入数据
		$ary = array();
		$ary["status"] = 0;
		
		$where = "id = $id";
		
		
		$ret = $this->update($this->table_name, $ary,$where);
		if ($ret)
		{
			/*$memcache = mem_cache::get_instance();
			$memcache->delete($key);*/
		}
		else
		{
			return false;
		}


		return $ret;
	}
	
	
	
	
	
	
	
		/**
	 * cron 获取food_tag
	 */
	function cron_get_gonglue_topic_count($start_id)
	{
		global $config;
		
		$start_id = intval($start_id);
		
		
		$where=" id > $start_id  and  status = 1 " ;
		$data = $this->get_listcount($this->table_name, $where);
		
		return $data;
	}
	

		/**
	 * cron 获取food_tag
	 */
	function cron_get_gonglue_topic_list($p,$pcount,$start_id)
	{
		global $config;
		
		$start_id = intval($start_id);
		$where=" id > $start_id  and  status = 1 " ;
		$data = $this->get_alllist($this->table_name,$p,$pcount,"id" , $where);
		
		return $data;
	}
	
	function cron_update_by_id($id,$cron_get_time)
	{
		if($cron_get_time)$ary["cron_get_time"] = $cron_get_time; 
		$where=" id=  $id " ;
		
		$data = $this->update($this->table_name,$ary , $where);
		
		return $data;
	}
	
	

}
